package com.halflife.nio;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class SelectorSocket {
    public static void main(String[] args) throws Exception{
        SelectorSocket socket = new SelectorSocket();
        socket.start();;
    }
    private Selector selector;
    private ServerSocketChannel server;

    public void init() throws Exception{
        selector = Selector.open();
        server = ServerSocketChannel.open();
        server.bind(new InetSocketAddress(9099));
        server.configureBlocking(false);
        server.register(selector, SelectionKey.OP_ACCEPT);
    }

    public void start() throws Exception{
        this.init();
        while (true) {
            if (selector.select(0) > 0) {
                System.out.println("-------------111-------------------");
                Set<SelectionKey> keys = selector.selectedKeys();//select keys
                Iterator<SelectionKey> ite = keys.iterator();
                while (ite.hasNext()) {
                    SelectionKey key = ite.next();
                    ite.remove();
                    if (key.isAcceptable()) {
                        ServerSocketChannel ss = (ServerSocketChannel) key.channel();
                        SocketChannel client = ss.accept();
                        client.configureBlocking(false);
                        client.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(8096));

                        System.out.println("accept:"+client.getRemoteAddress());
                    }
                    else if (key.isReadable()) {
                        SocketChannel client = (SocketChannel) key.channel();
                        System.out.println("read:"+client.getRemoteAddress());
                        ByteBuffer buffer = (ByteBuffer) key.attachment();
                        while (true) {
                            int i = client.read(buffer);
                            if (i > 0) {
                                buffer.flip();
                                byte[] bytes = new byte[buffer.limit()];
                                buffer.get(bytes);
                                System.out.println(new String(bytes));
                                buffer.clear();
                                buffer.put(bytes);
                                buffer.flip();
                                client.write(buffer);
                                buffer.clear();
                            } else if (i == 0) {
                                break;
                            }else if (i == -1) {
                                client.close();
                                break;
                            }
                        };
                    }
                }
            }
        }

    }
}
